package com.baselibrary.utils;

import android.app.Activity;
import android.widget.Toast;

import com.alibaba.android.arouter.utils.TextUtils;
import com.baselibrary.R;
import com.baselibrary.manager.LoadingManager;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;

/**
 * @author:hanke 2022/6/17
 * desc:
 */
public class UMShareUtils {
    public static UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            HKToastUtils.showToast("分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            HKToastUtils.showToast("分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            HKToastUtils.showToast("分享取消");
        }
    };

    public static void shareImageNet(Activity context, SHARE_MEDIA platform, String imgUrl) {
        if (TextUtils.isEmpty(imgUrl)) {
            ToastUtils.showShort(context, "分享参数有误");
        } else {
            UMImage imageUrl = new UMImage(context, imgUrl);
            imageUrl.setThumb(new UMImage(context, imgUrl));
            new ShareAction(context).withMedia(imageUrl)
                    .setPlatform(platform)
                    .setCallback(shareListener).share();
        }
    }

    public static void shareImageLocalFile(Activity context, SHARE_MEDIA platform, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            ToastUtils.showShort(context, "图片选择有误");
        } else {
            File file = new File(filePath);
            UMImage imageUrl = new UMImage(context, file);
            imageUrl.setThumb(new UMImage(context,BitmapUtils.getImageCompress(filePath)));
            new ShareAction(context).withMedia(imageUrl)
                    .setPlatform(platform)
                    .setCallback(shareListener).share();
        }
    }

    public static void shareWeb(Activity context, SHARE_MEDIA platform, String shareUrl, String imgUrl, String title, String content) {
        UMWeb web = new UMWeb(shareUrl);
        web.setTitle(title);//标题
        if (TextUtils.isEmpty(imgUrl)) {
            web.setThumb(new UMImage(context, R.drawable.ic_launcher));//缩略图
        } else {
            web.setThumb(new UMImage(context, imgUrl));//缩略图
        }
        web.setDescription(content);//描述
        new ShareAction(context).withMedia(web)
                .setPlatform(platform)
                .setCallback(shareListener).share();
    }

}
